{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom,bob,jane\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tom,bob,jane"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Company(object):\n",
    "    def __init__(self, employee_list):\n",
    "        self.employee = employee_list\n",
    "\n",
    "    def __getitem__(self, item):\n",
    "        return self.employee[item]\n",
    "\n",
    "    def __str__(self):\n",
    "        return ','.join(self.employee)\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return ','.join(self.employee)\n",
    "\n",
    "company = Company(['tom', 'bob', 'jane'])\n",
    "print(company)\n",
    "company"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Nums(object):\n",
    "    def __init__(self, num):\n",
    "        self.num = num\n",
    "        \n",
    "    def __abs__(self):\n",
    "        return abs(self.num)\n",
    "\n",
    "my_num = Nums(-1)\n",
    "abs(my_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 3, y: 5\n"
     ]
    }
   ],
   "source": [
    "## 魔法函数 __add__\n",
    "class MyVector(object):\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        \n",
    "    def __add__(self, other_instance):\n",
    "        re_vector = MyVector(self.x + other_instance.x, self.y + other_instance.y)\n",
    "        return re_vector\n",
    "    \n",
    "    def __str__(self):\n",
    "        return 'x: {x}, y: {y}'.format(x=self.x, y=self.y)\n",
    "\n",
    "vector1 = MyVector(1, 2)\n",
    "vector2 = MyVector(2, 3)\n",
    "print(vector1 + vector2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in __len__\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## len() 函数\n",
    "class Company(object):\n",
    "    def __init__(self, employee_list):\n",
    "        self.employee = employee_list\n",
    "    \n",
    "    def __len__(self):\n",
    "        print('in __len__')\n",
    "        return len(self.employee)\n",
    "    \n",
    "company = Company(['tom', 'bob', 'jane'])\n",
    "len(company)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
